﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Graphing;
using System.Threading;
using System.Windows.Threading;
using Graphing.Model;

namespace Graphing.Services
{
    public class ThreadingDepositoryService
    {
        #region Fields

        public IWindowOpenService openWinServ = GenericSingleton<IWindowOpenService>.GetInstance();
        public IWindowCloseService closeWinServ = GenericSingleton<IWindowCloseService>.GetInstance();
        public DrawPointService _drawPoint = GenericSingleton<DrawPointService>.GetInstance();
        public Scale graphScale = GenericSingleton<Scale>.GetInstance();
        public Dispatcher dispatch = Dispatcher.CurrentDispatcher;

        #endregion // Fields.

        #region OpenNewViewThread

        //Creates a thread to open a new view
        public void OpenNewViewThread(string viewName)
        {

         /*   Thread newViewThread = new Thread(new ParameterizedThreadStart(OpenView));
            newViewThread.SetApartmentState(ApartmentState.STA);
            newViewThread.IsBackground = true;
            newViewThread.Start(viewName);
          */

            ThreadStart action = () =>
                {
                    openWinServ.OpenView(viewName);

                    graphScale = new Scale(-10, 10, -10, 10);
                    _drawPoint.Redraw(graphScale);
                };
            DispatcherOperation test = dispatch.BeginInvoke(DispatcherPriority.Background, action);
        }

        #endregion // OpenNewViewThread.


        #region CloseViewThread

        //Thread that closes the specified view
        public void CloseViewThread(string viewName)
        {
            Action action = () =>
            {
                closeWinServ.CloseView(viewName);
            };
            dispatch.BeginInvoke(DispatcherPriority.Background, action);
        }

        #endregion // CloseViewThread.

    }
}
